package com.xxx.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.xxx.reggie.common.BaseContext;
import com.xxx.reggie.common.R;
import com.xxx.reggie.entity.AddressBook;
import com.xxx.reggie.service.AddressBookService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;

@RestController
@RequestMapping("/addressBook")
@Slf4j
public class AddressBookController {
    @Autowired
    private AddressBookService addressBookService;

    /**
     * 显示数据
     * @return
     */
    @GetMapping("/list")
    public R<List<AddressBook>> list(){
        //添加查询条件
        LambdaQueryWrapper<AddressBook> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(AddressBook::getUserId,BaseContext.getCurrentId());
        queryWrapper.orderByDesc(AddressBook::getCreateTime);
        List<AddressBook> list = addressBookService.list(queryWrapper);
        return R.success(list);
    }


    /**
     * 添加地址
     * @param addressBook
     * @return
     */
    @PostMapping
    public R<String> save(@RequestBody AddressBook addressBook){
        //获取用户id
        //使用的BaseContext.getCurrentID()获取用户id
        addressBook.setUserId(BaseContext.getCurrentId());
        //添加
        addressBookService.save(addressBook);
        return R.success("添加数据成功!!");
    }

    /**
     * 修改为默认地址
     * @param addressBook
     * @return
     */
    @PutMapping("/default")
    public R<AddressBook> setDefault(@RequestBody AddressBook addressBook){
        log.info("信息为->{}",addressBook);
        //将所有状态变为0
        LambdaQueryWrapper<AddressBook> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(AddressBook::getUserId,BaseContext.getCurrentId());
        addressBook.setIsDefault(0);
        addressBookService.update(addressBook,queryWrapper);
        //将目前的改为1
        addressBook.setIsDefault(1);
        addressBookService.updateById(addressBook);
        return R.success(addressBook);
    }

    /**
     * 回显数据
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public R<AddressBook> getViewDate(@PathVariable Long id){
        //按照id搜索数据
        //创建条件查询器
        LambdaQueryWrapper<AddressBook> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(id != null,AddressBook::getId,id);
        AddressBook one = addressBookService.getOne(queryWrapper);
        if (one != null) {
            return R.success(one);
        }
        return R.error("没有找到对象");
    }

    /**
     * 修改数据
     * @param addressBook
     * @return
     */
    @PutMapping
    public R<String> update(@RequestBody AddressBook addressBook){
        //修改数据
        //创建条件查询器
        if (addressBook.getId() != null){
            addressBookService.updateById(addressBook);
            return R.success("更新数据成功!!");
        }
        return R.error("更新数据失败");
    }

    /**
     * 删除地址
     * @param ids
     * @return
     */
    @DeleteMapping
    public R<String> delete(@RequestParam Long ids){
        //判断id是否存在
        LambdaQueryWrapper<AddressBook> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(AddressBook::getId,ids);
        if (addressBookService.getOne(queryWrapper) != null){
            //修改数据
            addressBookService.removeById(ids);
            return R.success("删除成功!!");
        }
        return R.error("删除失败");
    }

    @GetMapping("/default")
    public R<AddressBook> defaultAddress(){
        //创建查询条件器
        LambdaQueryWrapper<AddressBook> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(AddressBook::getUserId,BaseContext.getCurrentId());
        queryWrapper.eq(AddressBook::getIsDefault,1);
        AddressBook one = addressBookService.getOne(queryWrapper);
        return R.success(one);
    }
}
